package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    @Autowired
    private SystemPostService systemPostService;
    //系统用户信息
    @Override
    public void queryUserByPage(Page<SystemUserItemVo> page, SystemUserQueryVo queryVo) {
        systemUserMapper.selectSystemUserByPage(page,queryVo);
    }
    //根据ID查询后台用户信息
    @Override
    public SystemUserItemVo getSystemById(Long id) {
        SystemUserItemVo systemUserItemVo = systemUserMapper.costomQueryById(id);
        /*SystemUser systemUser = getById(id);
        systemUser.setPassword(null);
        SystemPost systemPost = systemPostService.getById(systemUser.getPostId());
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        BeanUtils.copyProperties(systemUser,systemUserItemVo);
        systemUserItemVo.setPostName(systemPost.getName());*/
        return systemUserItemVo;
    }

    //保存更新用户
    @Override
    public void costomSaveOrUpdate(SystemUser systemUser) {
        //1.检查账号问题（不能从重复 保存    更新都不能重复）
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper= new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername,systemUser.getUsername());
        lambdaQueryWrapper.ne(systemUser.getId()!=null,SystemUser::getId,systemUser.getId());
        long count = count(lambdaQueryWrapper);
        if(count>0){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR);
        }
        //2.密码加密
        if(!ObjectUtils.isEmpty(systemUser.getPassword())){
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }
        //3.调用保存更新方法
        saveOrUpdate(systemUser);
    }
}




